package com.youlai.boot.payment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.payment.entity.RechargeOrder;
import com.youlai.boot.payment.service.AlipayService;
import com.youlai.boot.system.model.form.RechargeForm;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 张雨润
 * @date 2025/4/23 19:00
 * @Description 支付宝支付控制器
 */
@Tag(name = "支付宝支付接口")
@RestController
@RequestMapping("/api/v1/payment/alipay")
@RequiredArgsConstructor
@Slf4j
public class AlipayController {

    private final AlipayService alipayService;

    @Operation(summary = "充值")
    @PostMapping("/recharge")
    public Result<Map<String, Object>> recharge(@RequestBody RechargeForm rechargeForm) {
        // 调用支付宝服务创建支付订单
        Map<String, Object> payResult = alipayService.createPayOrder(rechargeForm);
        return Result.success(payResult);
    }

    /**
     * 支付宝支付结果通知
     *
     * @param request 请求对象
     * @return 处理结果
     */
    @Operation(summary = "支付宝支付结果通知")
    @PostMapping("/notify")
    public String payNotify(HttpServletRequest request) {
        // 获取支付宝POST过来的反馈信息
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (String name : requestParams.keySet()) {
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }

        return alipayService.handlePayNotify(params);
    }

    /**
     * 查询支付订单状态
     *
     * @param orderNo 订单号
     * @return 订单状态
     */
    @Operation(summary = "查询支付订单状态")
    @GetMapping("/query/{orderNo}")
    public Result<String> queryPayStatus(@PathVariable String orderNo) {
        String status = alipayService.queryPayStatus(orderNo);
        return Result.success(status);
    }

    /**
     * 查询历史充值记录
     * 
     * @param userId
     * @return
     */
    @Operation(summary = "查询历史充值记录")
    @GetMapping("/query/history")
    public PageResult<RechargeOrder> queryRechargeList(@RequestParam(required = false) Long userId,
            @RequestParam(required = false) String username,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {
        IPage<RechargeOrder> result = alipayService.getRechargeList(userId, username, pageNum, pageSize);
        return PageResult.success(result);
    }

    /**
     * 将人民币转化为积分
     * 
     * @param rechargeForm
     * @return
     */
    @Operation(summary = "积分充值")
    @PostMapping("/recharge/exchange")
    public Result<Boolean> rechargeExchange(@RequestBody RechargeForm rechargeForm) {
        Boolean res = alipayService.rechargeExchange(rechargeForm);
        return Result.success(res);
    }

    /**
     * 账户余额提现
     * 
     * @param alipayAccount 支付宝账号
     * @param amount        提现金额
     * @return 提现结果
     */
    @Operation(summary = "账户提现")
    @PostMapping("/withdraw")
    public Result<Boolean> withdraw(
            @RequestParam String alipayAccount,
            @RequestParam String alipayRealName,
            @RequestParam BigDecimal amount) {
        Boolean res = alipayService.withdraw(alipayAccount, alipayRealName, amount);
        return Result.success(res);
    }

}